package BinaryTreePackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class preorderIteration_ {


  public List<Integer> Iteration(TreeNode root){
    List<Integer> result = new ArrayList<>();
    Stack<TreeNode> stack = new Stack<>();
    // 将根节点压入栈
    stack.push(root);
    while(!stack.isEmpty()){
      // 中间节点弹出栈
      TreeNode node = stack.pop();
      if (node != null){
        result.add(node.val);
      }
      else {
        continue;
      }
      // 将node的右子节点和左子节点分别压入栈
      stack.push(node.right);
      stack.push(node.left);
    }
    return result;
  }
}
